package org.rcy.framework.pay.config.ali;

import com.egzosn.pay.ali.api.AliPayConfigStorage;
import com.egzosn.pay.ali.api.AliPayService;
import com.egzosn.pay.ali.bean.AliTransactionType;
import com.egzosn.pay.ali.bean.AliTransferType;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.http.HttpConfigStorage;

import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 吴康桥
 * @Description
 * @date 2023/8/28 11:01
 */


public class AliPayServiceWrapper extends AliPayService {

	public AliPayServiceWrapper(AliPayConfigStorage payConfigStorage, HttpConfigStorage configStorage) {
		super(payConfigStorage, configStorage);
	}

	public AliPayServiceWrapper(AliPayConfigStorage payConfigStorage) {
		super(payConfigStorage);
	}

	public Map<String,Object> buildPayRequest(PayOrder order, String transactionType) throws IOException {
		Map<String,Object> map = new HashMap<>();
		AliTransactionType type = AliTransactionType.valueOf(transactionType);
		switch (type) {
			case APP: break;
			case PAGE:
				order.setTransactionType(AliTransactionType.PAGE);
				String resStr = super.buildRequest(super.orderInfo(order), MethodType.POST);
				map.put("result",resStr);
				map.put("produces","text/html;charset=UTF-8");
				break;
			case SWEEPPAY:
				order.setTransactionType(AliTransactionType.SWEEPPAY);
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ImageIO.write(super.genQrPay(order), "JPEG", baos);
				map.put("result",baos.toByteArray());
				map.put("produces","image/jpeg;charset=UTF-8");
				baos.close();
				break;
			case WAP:break;
			case MINAPP:break;
		}
		return map;
	}
}
